﻿using BW.Dto;
using BW.IBLL;
using BW.IDAL;
using BW.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BW.BLL
{
    public class MemberBLL : IMemberBLL
    {
        public MemberBLL(IBaseDAL<Member> memberDAL,IBaseDAL<MemberLevel> levelDAL)
        {
            MemberDAL = memberDAL;
            LevelDAL = levelDAL;
        }

        public IBaseDAL<Member> MemberDAL { get; }
        public IBaseDAL<MemberLevel> LevelDAL { get; }

        public async Task<int> AddMember(Member member)
        {
            //名称唯一
           bool exitis = MemberDAL.GetAllAsync().Any(x => member.Name == x.Name);
            if (exitis)
            {
                return -1;
            }
            else
            {
                return await MemberDAL.AddAsync(member);
            }
        }

        public async Task<int> AddRangeMember(List<Member> members)
        {
            return await MemberDAL.AddRangeAsync(members);
        }

        public IQueryable<Member> GetMemberList()
        {
            return MemberDAL.GetAllAsync();
        }

        public MemberPageWhereDto GetMemberPageWhere(int index, int size)
        {
            //本月在没有要求的时候，不允许显示出已删除的数据
            var members = MemberDAL.GetAllAsync().Where(x=>x.IsDelete==false);   //会员    延迟执行
            var levels = LevelDAL.GetAllAsync();//会员等级

            var linq = from m in members
                       join l in levels
                       on m.LevelId equals l.Id
                       select new
                       {
                           m.Id,
                           m.Name,
                           l.LevelName,
                           m.Img
                       };

            //先取总数
            int c = linq.Count();
            //分页查
            var d = linq.OrderBy(x => x.Id).Skip((index - 1) * size).Take(size).ToList();
            return new MemberPageWhereDto { Count = c, Members = d };
        }
         
        public Member GetMemberSingle(int id)
        {
            return MemberDAL.GetAllAsync().FirstOrDefault(x => x.Id == id);
        }


        /// <summary>
        /// 单个逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> LogiceDeleteMember(int id)
        {
             var m = MemberDAL.GetAllAsync().FirstOrDefault(x=>x.Id==id);
             m.DeleteDate= DateTime.Now;
             m.IsDelete= true;
            return await MemberDAL.UpDateAsync(m);
        }
        /// <summary>
        /// 批量逻辑删除
        /// </summary>
        /// <param name="idstring">由多个会员编号组成的字符串，如:"3,6,7,8,9"</param>
        /// <returns></returns>
        public async Task<int> LogiceRangeDeleteMember(string idstring)
        {
            //要把多个会员编号组成的字符串，如:"3,6,7,8,9"能过写代码变成整数的泛型集合 List<int>或整数的数组int[]
            //把字符串"3,6,7,8,9" =>  ["3","6","7","8","9"];
            //idstring.Split(",")
            //使用select把数组中每一个字符串转换成整数
            //idstring.Split(",").Select(x => Convert.ToInt32(x));
            List<int> list = idstring.Split(",").Select(x => Convert.ToInt32(x)).ToList();

            //根据多个会员编号集合找多个会员对象出来
            var mlist = MemberDAL.GetAllAsync().Where(x => list.Contains( x.Id) ).ToList(); // where id in (2,3,4,)
            foreach (var item in mlist)
            {
                item.DeleteDate = DateTime.Now;
                item.IsDelete = true;
            }
            return await  MemberDAL.UpDateRangeAsync(mlist);

        }

        public async Task<int> UpdateMember(Member member)
        {
            //名称唯一
            bool exitis = MemberDAL.GetAllAsync().Any(x => x.Name == member.Name && x.Id != member.Id);
            if (exitis)
            {
                return -1;
            }
            else
            {


                member.UpdateDate = DateTime.Now;
                return await MemberDAL.UpDateAsync(member);
            }

        }
    }
}
